package com.meisiabc.utils;

import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * Created by sunchangpeng
 */
public abstract class DateTimeUtil {
    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyyMMdd = "yyyyMMdd";
    public static final String yyyyMM = "yyyyMM";
    public static final String yyyy = "yyyy";
    public static final String MMdd = "MMdd";
    public static final String yyyyNianMMYueddRi = "yyyy年MM月dd日";

    public static DateTime maxOfDay(DateTime dateTime) {
        return dateTime.millisOfDay().withMaximumValue();
    }

    public static DateTime minOfDay(DateTime dateTime) {
        return dateTime.millisOfDay().withMinimumValue();
    }

    public static DateTime minOfMonth(DateTime dateTime) {
        return dateTime.dayOfMonth().withMinimumValue();
    }

    public static DateTime maxOfMonth(DateTime dateTime) {
        return dateTime.dayOfMonth().withMaximumValue();
    }

    public static DateTime parseDateTime(String time, String pattern) {
        DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern).withZoneUTC();
        return formatter.parseDateTime(time);
    }

    public static DateTime parseDateTime(String str) {
        return parseDateTime(str, yyyyMMddHHmmss);
    }

    public static int minutesbetween(DateTime begin, DateTime end) {
        Period p = new Period(begin, end, PeriodType.minutes());
        return p.getMinutes();
    }

    public static int daysbetween(DateTime begin, DateTime end) {
        Period p = new Period(begin, end, PeriodType.days());
        return p.getDays();
    }

    public static int daysbetweenMinOfDay(DateTime begin, DateTime end) {
        Period p = new Period(minOfDay(begin), minOfDay(end), PeriodType.days());
        return p.getDays();
    }

    public static int daysbetweenHuman(DateTime begin, DateTime end) {
        Period p = new Period(minOfDay(begin), minOfDay(end), PeriodType.days());
        return p.getDays() + 1;
    }
}